package com.example.zwh.computershop.home.views;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.zwh.computershop.Home_Activity;
import com.example.zwh.computershop.R;
import com.example.zwh.computershop.commodity.Commodity_goods;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Home_search extends AppCompatActivity {

    @BindView(R.id.home_btn_gotoback)
    LinearLayout homeBtnGotoback;
    @BindView(R.id.iv_search)
    ImageView ivSearch;//搜索的图片
    @BindView(R.id.home_tv_search_text)
    TextView homeTvSearchText;//文本输入框
    @BindView(R.id.lay_honme_search)
    LinearLayout layHonmeSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_search_activity);
        ButterKnife.bind(this);

        //顶部菜单栏屏蔽
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }

        //键盘监听
        homeTvSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //关闭键盘  上面的if是回车判断
                    View view = getWindow().peekDecorView();
                    if (view != null) {
                        InputMethodManager inputmanger = (InputMethodManager) getSystemService(Home_search.this.INPUT_METHOD_SERVICE);
                        inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
                    }
                    //跳转界面
                    gotoGommodity();
                    return true;
                }
                return false;
            }
        });
    }

    @OnClick({R.id.home_btn_gotoback, R.id.lay_honme_search, R.id.lay_honme_search_name})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.home_btn_gotoback:
                Intent intent = new Intent(this, Home_Activity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.lay_honme_search:
                break;
            case R.id.lay_honme_search_name:
                gotoGommodity();
                break;
        }
    }

    //跳转搜索结果界面
    private void gotoGommodity() {
        String inputText = homeTvSearchText.getText().toString();
//        Log.d("Shenyutian", "str:"+inputText);
        Intent intent = new Intent(Home_search.this, Commodity_goods.class);
        intent.putExtra("search_result",inputText);//带过去的数据
        startActivity(intent);
    }

}
